Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Runner that watches for Kubernetes APIs #4036

Merged
merged 5 commits into from
Nov 25, 2024

Conversation

cbandy
Copy link
Member

@cbandy cbandy commented Nov 19, 2024

This adds an internal/kubernetes package with types and functions for interrogating the Kubernetes API version and the APIs it has installed.

  • kubernetes.APIs is an interface for some read-only methods of k8s.io/…/sets.Set.
  • kubernetes.APISet implementes the APIs interface using a map.
  • kubernetes.DiscoveryRunner implements the APIs interface by periodically calling the Kubernetes API.

main() configures a DiscoveryRunner and adds it to the Manager and its Context so that the rest of our code can use it with these new functions:

  • kubernetes.Has(Context, API) bool
  • kubernetes.IsOpenShift(Context) bool
  • kubernetes.VersionString(Context) string

Checklist:

  • Have you added an explanation of what your changes do and why you'd like them to be included?
  • Have you updated or added documentation for the change, as applicable?
  • Have you tested your changes on all related environments with successful results, as applicable?
    • Have you added automated tests?

Type of Changes:

  • New feature
  • Testing enhancement

What is the current behavior (link to any open issues here)?

  • When VolumeSnapshots are enabled, we query for VolumeSnapshot APIs every PostgresCluster reconciliation.
    • When this query fails, reconciliation fails.
  • We query for OpenShift APIs once at startup.
  • We have "is OpenShift" logic in multiple controllers.

What is the new behavior (if this is a feature change)?

  • We query for a handful of APIs more than once and less than every reconciliation.
    • When this query fails, reconciliation continues unabated with old data.
  • Tests use Context and map to emulate different API configurations.
  • We have a canonical definition of "is OpenShift" with guidance for its use.
  • We have replaced one use of "is OpenShift" with a query for a specific API.

Other Information:

This is similar to #3973, but has grown to replace all our runtime use of the upstream discovery client.

@@ -293,9 +293,9 @@ func PodSecurityContext(cluster *v1beta1.PostgresCluster) *corev1.PodSecurityCon
// - https://cloud.redhat.com/blog/a-guide-to-openshift-and-uids
// - https://docs.k8s.io/tasks/configure-pod-container/security-context/
// - https://docs.openshift.com/container-platform/4.8/authentication/managing-security-context-constraints.html
if cluster.Spec.OpenShift == nil || !*cluster.Spec.OpenShift {
podSecurityContext.FSGroup = initialize.Int64(26)
if !initialize.FromPointer(cluster.Spec.OpenShift) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, OK, had to read the func / re-read the word "initialize" to get it: we give the value of the pointer or the null value of the type if the pointer is null.

So: Spec.OpenShift: true => true ; and false/nil => false.

Copy link
Member Author

@cbandy cbandy Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this call doesn't read well. A function that returns zero when a pointer is nil sounds intuitive, but I dunno in practice. Other functions in this package act on the pointers they're passed, so that combo is confusing, too.

K8s has a not-versioned pile of util with a ptr Deref(p *T, default T) T. That doesn't read much better to me.

runner.have.Version = version.Info{
Major: "1", Minor: "2", GitVersion: "asdf",
}
assert.Equal(t, "asdf", VersionString(NewAPIContext(ctx, runner)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

internal/kubernetes/apis.go Outdated Show resolved Hide resolved
@cbandy cbandy enabled auto-merge (rebase) November 25, 2024 22:42
@cbandy cbandy merged commit 90c8447 into CrunchyData:main Nov 25, 2024
15 of 16 checks passed
@cbandy cbandy deleted the discovery branch November 25, 2024 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants